package org.jeecg.modules.wlhy.hytaxdeclaration.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.third.dongjiang.tax.system.dto.ApiBaseResDTO;
import org.jeecg.modules.third.dongjiang.tax.system.dto.req.TempTaxpayerDTO;
import org.jeecg.modules.third.dongjiang.tax.system.dto.req.declaration.DeclarationDTO;
import org.jeecg.modules.third.dongjiang.tax.system.service.DongjiangTaxSystemService;
import org.jeecg.modules.wlhy.company.entity.HyCompany;
import org.jeecg.modules.wlhy.company.entity.HyCompanyConfig;
import org.jeecg.modules.wlhy.company.service.IHyCompanyConfigService;
import org.jeecg.modules.wlhy.company.service.IHyCompanyService;
import org.jeecg.modules.wlhy.hytaxdeclaration.entity.HyTaxDeclaration;
import org.jeecg.modules.wlhy.hytaxdeclaration.entity.HyTaxType;
import org.jeecg.modules.wlhy.hytaxdeclaration.mapper.HyTaxDeclarationMapper;
import org.jeecg.modules.wlhy.hytaxdeclaration.service.IHyTaxDeclarationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description: 车主申报信息
 * @Author: duanlikao
 * @Date:   2021-03-18
 * @Version: V1.0
 */
@Service
public class HyTaxDeclarationServiceImpl extends ServiceImpl<HyTaxDeclarationMapper, HyTaxDeclaration> implements IHyTaxDeclarationService {

    @Autowired
    private DongjiangTaxSystemService dongjiangTaxSystemService;

    @Resource
    private  HyTaxDeclarationMapper hyTaxDeclarationMapper;

    @Autowired
    private IHyCompanyConfigService companyConfigService;

    @Autowired
    private IHyCompanyService hyCompanyService;


    @Override
    public List <HyTaxType> queryTaxTypeData(){
        return hyTaxDeclarationMapper.queryTaxTypeData();
    }

    @Override
    public BigDecimal getYdzseTotal(String startTime, String endTime) {
        return hyTaxDeclarationMapper.getYdzseTotal( startTime,  endTime);
    }

    @Override
    public ApiBaseResDTO uploadTax(HyTaxDeclaration hyTaxDeclaration) {
        HyTaxDeclaration hyTaxDeclarationDb = getById(hyTaxDeclaration.getId());
        if(hyTaxDeclarationDb == null) {
            throw new JeecgBootException("未找到对应数据");
        }
        //if(hyTaxDeclarationDb.getUploadStatus().equals("1")){
        //    throw new JeecgBootException("该条数据已经上报："+hyTaxDeclarationDb.getId());
        //}
        DeclarationDTO declarationDTO = HyTaxDeclarationTODeclarationDTO(hyTaxDeclarationDb);
        if(declarationDTO == null){
            throw new JeecgBootException("未找到对应数据");
        }

        ApiBaseResDTO reportResult = dongjiangTaxSystemService.sendDeclaration(declarationDTO);

        Integer code =  StringUtils.isNotEmpty(reportResult.getCode())? Integer.valueOf(reportResult.getCode()) : 0;

        hyTaxDeclarationDb.setUploadStatus(code >= 100 && code < 200  ? "1" :"2");
        hyTaxDeclarationDb.setUploadDate(new Date());
        this.updateById(hyTaxDeclarationDb);
        return reportResult;
    }

    public static DeclarationDTO HyTaxDeclarationTODeclarationDTO(HyTaxDeclaration hyTaxDeclaration) {
        if (hyTaxDeclaration == null) {
            return null;
        }
        DeclarationDTO declarationDTO = new DeclarationDTO();
        declarationDTO.setFwfuuid(hyTaxDeclaration.getFwfuuid());
        declarationDTO.setBdzdwnsrsbh(hyTaxDeclaration.getBdzdwnsrsbh());
        declarationDTO.setBdzdwnsrmc(hyTaxDeclaration.getBdzdwnsrmc());
        declarationDTO.setBdzcbsbh("");// 否 被代征船舶识别号
        declarationDTO.setBdzcbmc("");// 否 被代征船舶名称
        declarationDTO.setGjhdq(hyTaxDeclaration.getGjhdq());
        declarationDTO.setZjlx(hyTaxDeclaration.getZjlx());
        declarationDTO.setZjhm(hyTaxDeclaration.getZjhm());
        declarationDTO.setSshy(hyTaxDeclaration.getSshy());
        declarationDTO.setZsxm(hyTaxDeclaration.getZsxm());
        declarationDTO.setZszm("");// 是 征收子目
        declarationDTO.setZspm(hyTaxDeclaration.getZspm());
        declarationDTO.setZszm(hyTaxDeclaration.getZszm());
        declarationDTO.setSkssqq(
                DateUtils.date2Str(  hyTaxDeclaration.getSkssqq(),DateUtils.date_sdf.get())
        );
        declarationDTO.setSkssqz(
                DateUtils.date2Str(  hyTaxDeclaration.getSkssqz(),DateUtils.date_sdf.get())
        );
        declarationDTO.setJsyj(String.format("%.2f", hyTaxDeclaration.getJsyj()));
        declarationDTO.setSlhse(String.format("%.4f", hyTaxDeclaration.getSlhse()));
        declarationDTO.setYnse(String.format("%.2f", hyTaxDeclaration.getYnse()));
        declarationDTO.setJmse(String.format("%.2f", hyTaxDeclaration.getJmse()));
        declarationDTO.setYjse(String.format("%.2f", hyTaxDeclaration.getYjse()));
        declarationDTO.setYdzse(
                hyTaxDeclaration.getYdzse() != null ?   String.format("%.2f", hyTaxDeclaration.getYdzse()):
                String.valueOf(00.00)
        );
        declarationDTO.setSdzse(
                hyTaxDeclaration.getSdzse() != null ?  String.format("%.2f", hyTaxDeclaration.getSdzse()):
                String.valueOf(00.00)
        );
        declarationDTO.setSybz("");// 否 税源标志
        declarationDTO.setSybh("");// 否 税源编号
        declarationDTO.setSyzl("");// 否 税源坐落
        declarationDTO.setJmxz(hyTaxDeclaration.getJmxz());
        return declarationDTO;
    }

    @Override
    public ApiBaseResDTO sendTempTaxpayer(HyTaxDeclaration hyTaxDeclaration) {
        HyTaxDeclaration hyTaxDeclarationDb = this.getById(hyTaxDeclaration.getId());
        if(hyTaxDeclarationDb == null) {
            throw new JeecgBootException("未找到对应数据");
        }
        if(!"0001045308".equals(hyTaxDeclarationDb.getJmxz())){
            throw new JeecgBootException(hyTaxDeclaration.getBdzdwnsrmc()+": 该条数据不需要上报纳税人信息");
        }

//        if(hyTaxDeclarationDb.getUploadStatus().equals("1")){
//            throw new JeecgBootException("该条数据已经上报："+hyTaxDeclarationDb.getId());
//        }

        List<HyCompanyConfig> companyConfigs = companyConfigService.list(new QueryWrapper<HyCompanyConfig>()
                .eq("tax_upload_type", 1)
                .eq("tj_tax_upload_status","1")
        );
        if(companyConfigs.size() == 0 ){
            throw new JeecgBootException("上报公司信息未配置!");
        }

        HyCompanyConfig companyConfig = companyConfigs.get(0);
        HyCompany company  = hyCompanyService.getById(companyConfig.getCompanyId());
        TempTaxpayerDTO tempTaxpayerDTO = HyTaxDeclarationTOTempTaxpayerDTO(hyTaxDeclarationDb);
        if(tempTaxpayerDTO == null){
            throw new JeecgBootException("未找到对应数据");
        }

        tempTaxpayerDTO.setScjydz(company.getScjydz());
        tempTaxpayerDTO.setJyfw(company.getRunScope());
        tempTaxpayerDTO.setJbrxm(company.getBsrxm());
        tempTaxpayerDTO.setJbrsfzjhm(company.getBsrsfzjhm());
        tempTaxpayerDTO.setJbrsfzjlx(company.getBsrsfzjzl());
        ApiBaseResDTO reportResult = dongjiangTaxSystemService.sendTempTaxpayer(tempTaxpayerDTO);

        Integer code =  StringUtils.isNotEmpty(reportResult.getCode())? Integer.valueOf(reportResult.getCode()) : 0;
        hyTaxDeclarationDb.setTempTaxpayerUploadStatus(code >= 100 && code < 200  ? "1" :"2");
        hyTaxDeclarationDb.setTempTaxpayerUploadDate(new Date());
        this.updateById(hyTaxDeclarationDb);
        return reportResult;
    }
    public static TempTaxpayerDTO HyTaxDeclarationTOTempTaxpayerDTO(HyTaxDeclaration hyTaxDeclaration){
        if (hyTaxDeclaration == null) {
            return null;
        }
        TempTaxpayerDTO tempTaxpayerDTO = new TempTaxpayerDTO();
        tempTaxpayerDTO.setFwfuuid(hyTaxDeclaration.getFwfuuid());
        tempTaxpayerDTO.setNsrmc(hyTaxDeclaration.getBdzdwnsrmc());
        tempTaxpayerDTO.setNsrsbh(hyTaxDeclaration.getBdzdwnsrsbh());
        tempTaxpayerDTO.setFddbrxm(hyTaxDeclaration.getBdzdwnsrmc());
        tempTaxpayerDTO.setFddbrsfzjhm(hyTaxDeclaration.getZjhm());
        tempTaxpayerDTO.setFddbrsfzjzl(hyTaxDeclaration.getZjlx());
        return tempTaxpayerDTO;
    }
}
